<?php
namespace Client\Controller;
use Think\Controller; 
use Org\Net\Http;
class CollectController extends Controller {
	/**
	 * 收藏
	 */
    public function addCollect(){
    	if(IS_POST){
	    	$collect = M ( "Collect" );
	    	$aid = I('aid');
			$type = I ( 'type' ) == '' ? "Scenic" : I ( 'type' );
			$scenic = D ( $type );
			$arr = $scenic->where ( array (
					'id' => I ( 'content' ) 
			) )->find ();
			$data ['title'] = $arr ['subtitle'];
			$data ['name'] = $arr ['name'];
			$data ['image'] = split ( ",", $arr ['image'] ) [0];
			$data ['aid'] = $aid;
			$result = $collect->add ( $data );
			if ($result) {
				S("Collect_{$aid}",null);
				$json['result'] = 0;
				$json['msg'] = "添加成功";
			} else {
				$json['result'] = -1;
				$json['msg'] = "添加失败";
			}
			$this->ajaxReturn($json);
    	}
    }
    /**
     * 获取收藏信息
     */
    public function  getCollect(){
    	if(IS_POST){
    		$aid = I('aid');
    		$list = S("Collect_{$aid}");
    		$where['aid'] = $aid;
    		$pageSize = 10;
    		$page = I('page');
    		if(isset($page)){
    			$start = $page*$pageSize;
    		}
    		if(!$list){
    			$list = M('Collect')->cache("Collect_{$aid}")->where($where)->order('id desc')->select();
    		}
    		$res = array_slice($list,$start,$pageSize);
    		if(count($res)>0){
    			$json['result'] = 0;
    			$json['msg'] = "成功";
    			$json['collect'] = $res;
    		}else{
    			$json['result'] = -1;
    			$json['msg'] = "暂无推荐";
    		}
    		$this->ajaxReturn($json);
    	}
    }
    /**
     * 取消收藏
     */
    public function cancelCollect(){
    	if(IS_POST){
    		$where['aid'] = I('aid');
    		$where['content'] = I('content');
    		$result = M('Collect')->where($where)->delete();
    		if ($result) {
    			S("Collect_{$aid}",null);
    			$json['result'] = 0;
    			$json['msg'] = "取消收藏成功";
    		} else {
    			$json['result'] = -1;
    			$json['msg'] = "取消收藏失败";
    		}
    		$this->ajaxReturn($json);
    	}
    }
    
}